package com.primecodegen.common.GeneratedTargets;

/**
 *----------------------------------------------------------
 *----------------------------------------------------------
 * @author Mohammad Awwad
 * Version: v0.0
 * contact: mhmdawwad@gmail.com
 *-----------------------------------------------------------
 *-----------------------------------------------------------
 */
import java.sql.*;
import java.util.*;
import com.primecodegen.common.base.*;
import com.primecodegen.util.*;
import java.util.logging.*;

public class GeneratedTargetsDAO extends BaseDAO {

    protected static String TblName = "Generated_Targets";
    protected java.sql.Connection generatedTargetsConn;
    protected static final Logger logger = Logger.getLogger(GeneratedTargetsDAO.class.getName());

    public GeneratedTargetsDAO() {
        super();
    }

    public int insert(IBaseTable generatedTargetsTable) throws Exception {


        final boolean isConnSupplied = (generatedTargetsConn != null);
        GeneratedTargetsTable newRecord = (GeneratedTargetsTable) generatedTargetsTable;
        int insertValue = ComConstants.UNREALISTIC_INT;
        Connection con = null;
        PreparedStatement PrepSt = null;
        String sInsertSQL = null;
        StringBuffer sbSQL = new StringBuffer();

        if (generatedTargetsTable == null) {
            throw new Exception("DAOException in GeneratedTargetsDAO class :generatedTargetsTable is Null");
        }
        sInsertSQL = "INSERT INTO " + GeneratedTargetsDAO.TblName + " (";
        try {
            con = isConnSupplied ? generatedTargetsConn : ResourceManager.getConnection();

            String sComma = "";
            int iColCount = 0;


            if (!util.isEmpty(newRecord.getID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.ID);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getFilePostName())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.FILEPOSTNAME);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getFilePreName())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.FILEPRENAME);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getGenerateTo())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.GENERATETO);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getTemplate())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.TEMPLATE);
                sComma = ",";
            }

            if (!util.isEmpty(newRecord.getMainProject_ID())) {
                iColCount = iColCount + 1;
                sbSQL.append(sComma).append(GeneratedTargetsDBCols.MAINPROJECT_ID);
                sComma = ",";
            }


            sbSQL.append(") VALUES (");
            sComma = "";
            for (int i = 0; i < iColCount; i++) {
                sbSQL.append(sComma).append("?");
                sComma = ",";
            }
            sbSQL.append(");");

            sInsertSQL = sInsertSQL + sbSQL.toString();

            PrepSt = con.prepareStatement(sInsertSQL,
                    Statement.RETURN_GENERATED_KEYS);
            int i = 1;



            if (!util.isEmpty(newRecord.getID())) {
                PrepSt.setInt(i++, newRecord.getID());
            }

            if (!util.isEmpty(newRecord.getFilePostName().trim())) {
                PrepSt.setString(i++, newRecord.getFilePostName().trim());
            }

            if (!util.isEmpty(newRecord.getFilePreName().trim())) {
                PrepSt.setString(i++, newRecord.getFilePreName().trim());
            }

            if (!util.isEmpty(newRecord.getGenerateTo().trim())) {
                PrepSt.setString(i++, newRecord.getGenerateTo().trim());
            }

            if (!util.isEmpty(newRecord.getTemplate().trim())) {
                PrepSt.setString(i++, newRecord.getTemplate().trim());
            }

            if (!util.isEmpty(newRecord.getMainProject_ID())) {
                PrepSt.setInt(i++, newRecord.getMainProject_ID());
            }


            insertValue = PrepSt.executeUpdate();
            ResultSet rsIDJustInserted = PrepSt.getGeneratedKeys();
            if (rsIDJustInserted.next()) {
                insertValue = rsIDJustInserted.getInt(1);
            }



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in GeneratedTargetsDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in GeneratedTargetsDAO.insert method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in GeneratedTargetsDAO.insert method Message: {0}", e.getMessage());
            throw new Exception("Exception in GeneratedTargetsDAO.insert method");
        } finally {
            ResourceManager.close(PrepSt);
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return insertValue;
    }

    public int deleteByID(int IDPar) throws Exception {

        final boolean isConnSupplied = (generatedTargetsConn != null);
        int ID = (int) IDPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        int noOfRowsDeleted = ComConstants.UNREALISTIC_INT;
        String deleteSQL = "DELETE FROM " + GeneratedTargetsDAO.TblName + " WHERE " + GeneratedTargetsDBCols.ID + " = ?";
        try {
            con = isConnSupplied ? generatedTargetsConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(deleteSQL);
            stmnt.setInt(1, ID);

            noOfRowsDeleted = stmnt.executeUpdate();



        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in GeneratedTargetsDAO.DeleteByID method Message: {0}", sqlEx.getMessage());
            throw new Exception("SQLException in GeneratedTargetsDAO.deleteByID method"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in GeneratedTargetsDAO.DeleteByID method Message: {0}", e.getMessage());
            throw new Exception("Exception in GeneratedTargetsDAO.deleteByID method"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return noOfRowsDeleted;
    }

    public int updateByCriterion(IBaseTable generatedTargetsSrchCriterionDTOPar,
            IBaseTable updatedRecordPar) throws Exception {

        final boolean isConnSupplied = (generatedTargetsConn != null);
        GeneratedTargetsTable generatedTargetsSrchCriterionDTO = (GeneratedTargetsTable) generatedTargetsSrchCriterionDTOPar;
        GeneratedTargetsTable updatedRecord = (GeneratedTargetsTable) updatedRecordPar;
        int iNoOfRecordsUpdated = -1;
        Connection con = null;
        PreparedStatement stmnt = null;
        try {

            con = isConnSupplied ? generatedTargetsConn : ResourceManager.getConnection();
            StringBuffer sbUpdateSQLStmnt = new StringBuffer();
            String sComma = "";
            sbUpdateSQLStmnt.append("UPDATE ").append(GeneratedTargetsDAO.TblName).append(" SET ");



            if (!util.isEmpty(updatedRecord.getID())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.ID + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getFilePostName())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.FILEPOSTNAME + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getFilePreName())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.FILEPRENAME + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getGenerateTo())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.GENERATETO + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getTemplate())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.TEMPLATE + " = ").append("?");
                sComma = ",";
            }

            if (!util.isEmpty(updatedRecord.getMainProject_ID())) {
                sbUpdateSQLStmnt.append(sComma).append(GeneratedTargetsDBCols.MAINPROJECT_ID + " = ").append("?");
                sComma = ",";
            }


            sbUpdateSQLStmnt.append(getUpdateWhereClause(generatedTargetsSrchCriterionDTO));

            stmnt = con.prepareStatement(sbUpdateSQLStmnt.toString());
            int i = 1;

            if (!util.isEmpty(updatedRecord.getID())) {
                stmnt.setInt(i++, updatedRecord.getID());
            }

            if (!util.isEmpty(updatedRecord.getFilePostName())) {
                stmnt.setString(i++, updatedRecord.getFilePostName());
            }

            if (!util.isEmpty(updatedRecord.getFilePreName())) {
                stmnt.setString(i++, updatedRecord.getFilePreName());
            }

            if (!util.isEmpty(updatedRecord.getGenerateTo())) {
                stmnt.setString(i++, updatedRecord.getGenerateTo());
            }

            if (!util.isEmpty(updatedRecord.getTemplate())) {
                stmnt.setString(i++, updatedRecord.getTemplate());
            }

            if (!util.isEmpty(updatedRecord.getMainProject_ID())) {
                stmnt.setInt(i++, updatedRecord.getMainProject_ID());
            }



            iNoOfRecordsUpdated = stmnt.executeUpdate();

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in GeneratedTargetsDAO.updateByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in GeneratedTargetsDAO.updateByCriterion method ");
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }

        return iNoOfRecordsUpdated;
    }

    public int updateByID(IBaseTable newRecordPar) throws Exception {
        GeneratedTargetsTable newRecord = (GeneratedTargetsTable) newRecordPar;
        if (newRecord != null) {
            GeneratedTargetsTable contentsCategorySrchCriterionDTO = new GeneratedTargetsTable();
            contentsCategorySrchCriterionDTO.setID(newRecord.getID());
            return this.updateByCriterion(contentsCategorySrchCriterionDTO, newRecord);
        } else {
            logger.log(Level.WARNING, "Exception in method GeneratedTargetsDAO.update.newRecord passed is null.");
            throw new Exception(
                    "Exception in method GeneratedTargetsDAO.update.newRecord passed is null.");
        }
    }

    private String getUpdateWhereClause(GeneratedTargetsTable generatedTargetsSrchCriterionDTO) {
        String sWhere = "";
        if (generatedTargetsSrchCriterionDTO.getID() != ComConstants.UNREALISTIC_INT) {
            sWhere = " WHERE ID = " + generatedTargetsSrchCriterionDTO.getID();
        }
        return sWhere;
    }

    public IBaseTable getByID(int ID) throws Exception {

        if (!util.isEmpty(ID)) {
            GeneratedTargetsTable generatedTargetsSrchCriterionDTO = new GeneratedTargetsTable();
            generatedTargetsSrchCriterionDTO.setID(ID);
            generatedTargetsSrchCriterionDTO.setFilter(true);
            ArrayList<GeneratedTargetsTable> generatedTargetsTables = this.getByCriterionOrderedBy(
                    generatedTargetsSrchCriterionDTO, null);
            GeneratedTargetsTable generatedTargetsTable = null;
            if (generatedTargetsTables != null) {
                Iterator<GeneratedTargetsTable> it = generatedTargetsTables.iterator();
                while (it.hasNext()) {
                    generatedTargetsTable = (GeneratedTargetsTable) it.next();
                }
            }

            return generatedTargetsTable;
        } else {
            logger.log(Level.WARNING, "Exception in method GeneratedTargetsDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method GeneratedTargetsDAO.getByID.ID passed is unrealistic.");
        }
    }

    public boolean deleteMainProjectIsSafe(int mainProjectID) throws Exception {
        ArrayList<GeneratedTargetsTable> generatedTargetsTables = new ArrayList<GeneratedTargetsTable>();
        if (!util.isEmpty(mainProjectID)) {
            GeneratedTargetsTable generatedTargetsSrchCriterionDTO = new GeneratedTargetsTable();
            generatedTargetsSrchCriterionDTO.setMainProject_ID(mainProjectID);
            generatedTargetsSrchCriterionDTO.setFilter(true);
            generatedTargetsTables = this.getByCriterionOrderedBy(generatedTargetsSrchCriterionDTO, null);

            if (generatedTargetsTables.size() > 0) {
                return false;//Delete is SAFE, Group Empty
            } else {
                return true;
            }

        } else {
            logger.log(Level.WARNING, "Exception in method GeneratedTargetsDAO.getByID.ID passed is unrealistic.");
            throw new Exception(
                    "Exception in method GeneratedTargetsDAO.getByID.ID passed is unrealistic.");
        }
    }

    public ArrayList<GeneratedTargetsTable> getByCriterionOrderedBy(
            IBaseTable generatedTargetsSrchCriterionDTOPar, ColToOrderBy colToOrderBy)
            throws Exception {


        final boolean isConnSupplied = (generatedTargetsConn != null);
        GeneratedTargetsTable generatedTargetsSrchCriterionDTO = (GeneratedTargetsTable) generatedTargetsSrchCriterionDTOPar;
        Connection con = null;
        PreparedStatement stmnt = null;
        ResultSet rs = null;

        String sWhereClause = "";

        String sOrderBy = "";
        if (colToOrderBy != null && colToOrderBy.Name != null) {
            sOrderBy = this.getOrderBy(colToOrderBy);
        }
        if (generatedTargetsSrchCriterionDTO != null && generatedTargetsSrchCriterionDTO.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(generatedTargetsSrchCriterionDTO);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" GeneratedTargets.getByCriterionOrderedBy.Exception:  " + e.getMessage());
            }

            if (sWhereClause != null && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }
        }
        ArrayList<GeneratedTargetsTable> generatedTargetsTables = new ArrayList<GeneratedTargetsTable>();

        try {
            con = isConnSupplied ? generatedTargetsConn : ResourceManager.getConnection();

            String sSelectSQL = "SELECT * FROM   " + TblName;
//                       StringBuilder sSelectSQL = new StringBuilder();
//                        sSelectSQL.append("SELECT ").append(" roles.ID ").append(" AS ").append(RolesDBCols.ID).append(", roles.Code ").append(" AS ").append(RolesDBCols.CODE).append(", roles.Name ").append(" AS ").append(RolesDBCols.NAME).append(", roles.Description ").append(" AS ").append(RolesDBCols.DESCRIPTION).append(", roles.Details ").append(" AS ").append(RolesDBCols.DETAILS).append(", roles.IsActive ").append(" AS ").append(RolesDBCols.ISACTIVE).append(", roles.IsVisible ").append(" AS ").append(RolesDBCols.ISVISIBLE).append(", roles_group.ID ").append(" AS ").append(RolesDBCols.ROLEGROUPID).append(", roles_group.GroupName ").append(" AS ").append(RolesDBCols.ROLEGROUPNAME).append(" FROM ").append(" roles ").append("INNER JOIN roles_group ON (roles.RolesGroupID = roles_group.ID)");

            stmnt = con.prepareStatement(sSelectSQL + sWhereClause + sOrderBy);




            if (generatedTargetsSrchCriterionDTO != null && generatedTargetsSrchCriterionDTO.isFilter()) {
                int colCount = 1;


                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getID())) {
                    stmnt.setInt(colCount++, generatedTargetsSrchCriterionDTO.getID());
                }

                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getFilePostName())) {
                    stmnt.setString(colCount++, generatedTargetsSrchCriterionDTO.getFilePostName());
                }

                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getFilePreName())) {
                    stmnt.setString(colCount++, generatedTargetsSrchCriterionDTO.getFilePreName());
                }

                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getGenerateTo())) {
                    stmnt.setString(colCount++, generatedTargetsSrchCriterionDTO.getGenerateTo());
                }

                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getTemplate())) {
                    stmnt.setString(colCount++, generatedTargetsSrchCriterionDTO.getTemplate());
                }

                if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getMainProject_ID())) {
                    stmnt.setInt(colCount++, generatedTargetsSrchCriterionDTO.getMainProject_ID());
                }

            }

            rs = stmnt.executeQuery();
            GeneratedTargetsTable generatedTargetsTable = null;

            while (rs.next()) {
                generatedTargetsTable = new GeneratedTargetsTable();



                generatedTargetsTable.setID(rs.getInt(GeneratedTargetsDBCols.ID));


                generatedTargetsTable.setFilePostName(util.removeNullFrom(rs.getString(GeneratedTargetsDBCols.FILEPOSTNAME)));


                generatedTargetsTable.setFilePreName(util.removeNullFrom(rs.getString(GeneratedTargetsDBCols.FILEPRENAME)));


                generatedTargetsTable.setGenerateTo(util.removeNullFrom(rs.getString(GeneratedTargetsDBCols.GENERATETO)));


                generatedTargetsTable.setTemplate(util.removeNullFrom(rs.getString(GeneratedTargetsDBCols.TEMPLATE)));


                generatedTargetsTable.setMainProject_ID(rs.getInt(GeneratedTargetsDBCols.MAINPROJECT_ID));


                generatedTargetsTables.add(generatedTargetsTable);
                generatedTargetsTable = null;
            }

        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in GeneratedTargetsDAO.insert method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in method:GeneratedTargetsDAO.getByCriterionOrderedBy"
                    + sqlEx.getMessage());
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in GeneratedTargetsDAO.insert method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in method:GeneratedTargetsDAO.getByCriterionOrderedBy"
                    + e.getMessage());
        } finally {
            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
        return generatedTargetsTables;
    }

    private String getRetrieveWhereClause(GeneratedTargetsTable generatedTargetsSrchCriterionDTO)
            throws Exception {


        StringBuffer sbWhereClause = new StringBuffer();
        String sAnd = "";
        boolean bPrevColExistsInWhereClause = false;
        boolean bAtLeastOneFieldExists = false;

        if (!util.isEmpty(generatedTargetsSrchCriterionDTO.getID())) {
            bAtLeastOneFieldExists = true;
            sbWhereClause.append(" ID =").append(" ? ");
            bPrevColExistsInWhereClause = true;
        }

        if (generatedTargetsSrchCriterionDTO.Opr != null) {



  


            if (!util.isEmpty(generatedTargetsSrchCriterionDTO.Opr.getFilePostNameOpr().trim()) && !util.isEmpty(generatedTargetsSrchCriterionDTO.getFilePostName().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (generatedTargetsSrchCriterionDTO.Opr.getFilePostNameOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.FILEPOSTNAME + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.FILEPOSTNAME + "  ").append(generatedTargetsSrchCriterionDTO.Opr.getFilePostNameOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(generatedTargetsSrchCriterionDTO.Opr.getFilePreNameOpr().trim()) && !util.isEmpty(generatedTargetsSrchCriterionDTO.getFilePreName().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (generatedTargetsSrchCriterionDTO.Opr.getFilePreNameOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.FILEPRENAME + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.FILEPRENAME + "  ").append(generatedTargetsSrchCriterionDTO.Opr.getFilePreNameOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(generatedTargetsSrchCriterionDTO.Opr.getGenerateToOpr().trim()) && !util.isEmpty(generatedTargetsSrchCriterionDTO.getGenerateTo().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (generatedTargetsSrchCriterionDTO.Opr.getGenerateToOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.GENERATETO + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.GENERATETO + "  ").append(generatedTargetsSrchCriterionDTO.Opr.getGenerateToOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(generatedTargetsSrchCriterionDTO.Opr.getTemplateOpr().trim()) && !util.isEmpty(generatedTargetsSrchCriterionDTO.getTemplate().trim())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                if (generatedTargetsSrchCriterionDTO.Opr.getTemplateOpr().trim().equalsIgnoreCase("LIKE")) {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.TEMPLATE + " LIKE").append(" ? ");
                } else {
                    sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.TEMPLATE + "  ").append(generatedTargetsSrchCriterionDTO.Opr.getTemplateOpr().trim()).append("?");
                }
                bPrevColExistsInWhereClause = true;
            }


            if (!util.isEmpty(generatedTargetsSrchCriterionDTO.Opr.getMainProject_IDOpr().trim()) && !util.isEmpty(generatedTargetsSrchCriterionDTO.getMainProject_ID())) {
                bAtLeastOneFieldExists = true;
                if (bPrevColExistsInWhereClause) {
                    sAnd = " AND ";
                } else {
                    sAnd = "";
                }
                sbWhereClause.append(sAnd).append(" " + GeneratedTargetsDBCols.MAINPROJECT_ID + "  ").append(generatedTargetsSrchCriterionDTO.Opr.getMainProject_IDOpr().trim()).append("?");
                bPrevColExistsInWhereClause = true;
            }




        }

        if (sbWhereClause.toString().trim().length() <= 0) {
            logger.log(Level.WARNING, "getRetrieveWhereClause Cannot return Null  ");
            throw new Exception("getRetrieveWhereClause Cannot return Null  ");
        } else {
            return sbWhereClause.toString().trim();
        }
    }

    private String getOrderBy(ColToOrderBy colToOrderBy) {
        String sOrderBy = " Order By ";

        sOrderBy = sOrderBy + colToOrderBy.Name;

        if (colToOrderBy.OrderType != null) {
            sOrderBy = sOrderBy + " " + colToOrderBy.OrderType;
        }
        return sOrderBy;
    }

    public int deleteByCriterion(IBaseTable recordsToDeleteCritPar)
            throws Exception {

        final boolean isConnSupplied = (generatedTargetsConn != null);
        GeneratedTargetsTable recordsToDeleteCrit = (GeneratedTargetsTable) recordsToDeleteCritPar;
        int iNoOfRecordsDeleted = -1;
        Connection con = null;
        PreparedStatement stmnt = null;

        StringBuffer sbDeleteSQLStmnt = new StringBuffer();
        sbDeleteSQLStmnt.append("DELETE from ").append(GeneratedTargetsDAO.TblName).append(" WHERE ");

        String sWhereClause = "";
        if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            try {
                sWhereClause = this.getRetrieveWhereClause(recordsToDeleteCrit);
            } catch (Exception e) {
                System.out.println("getByCriterionOrderedBy.Exception..");
                throw new Exception(" GeneratedTargets.deleteByCriterion.Exception:  "
                        + e.getMessage());
            }

            if (sWhereClause != null
                    && !sWhereClause.trim().equalsIgnoreCase("")) {
                sWhereClause = " WHERE " + sWhereClause;
            }

            sbDeleteSQLStmnt.append(sWhereClause);
        }
        try {
            con = isConnSupplied ? generatedTargetsConn : ResourceManager.getConnection();
            stmnt = con.prepareStatement(sbDeleteSQLStmnt.toString());

            if (recordsToDeleteCrit != null && recordsToDeleteCrit.isFilter()) {
            }
            //  iNoOfRecordsDeleted = stmnt.executeUpdate ();



            return iNoOfRecordsDeleted;
        } catch (SQLException sqlEx) {
            logger.log(Level.WARNING, "SQLException in GeneratedTargetsDAO.deleteByCriterion method Message: {0}", sqlEx.getMessage());
            throw new Exception(
                    "SQLException in GeneratedTargetsDAO.deleteByCriterion method");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in GeneratedTargetsDAO.deleteByCriterion method Message: {0}", e.getMessage());
            throw new Exception(
                    "Exception in GeneratedTargetsDAO.deleteByCriterion method");
        } finally {

            if (!isConnSupplied) {
                ResourceManager.close(con);
            }
        }
    }
}
